package com.xywl.escort.adapter;

import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xywl.escort.R;
import com.xywl.escort.bean.PurchaseOrderDetailBean;

import java.util.List;

import timber.log.Timber;

public class OrderEvaluateAdapter extends BaseQuickAdapter<PurchaseOrderDetailBean.OrderItemsBean, BaseViewHolder> {
    public OrderEvaluateAdapter(int layoutResId, @Nullable List<PurchaseOrderDetailBean.OrderItemsBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, final PurchaseOrderDetailBean.OrderItemsBean item) {
        helper.setText(R.id.text_title, item.spuName);
        final EditText editText = helper.getView(R.id.edit_evaluate);
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }
        TextView textEvaluate = helper.getView(R.id.text_evaluate);
        TextView textSubmit = helper.getView(R.id.text_submit);
        Timber.e("item.commentId=" + item.commentId);
        if (!TextUtils.isEmpty(item.commentId)) {
            textEvaluate.setVisibility(View.VISIBLE);
            textEvaluate.setText(item.commentContent);
            editText.setVisibility(View.GONE);
            textSubmit.setVisibility(View.GONE);
        } else {
            textEvaluate.setVisibility(View.GONE);
            editText.setVisibility(View.VISIBLE);
            textSubmit.setVisibility(View.VISIBLE);
            editText.setText(item.commentContent);

            TextWatcher watcher = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {
                    item.commentContent = editText.getText().toString();
                }
            };

            editText.addTextChangedListener(watcher);
            editText.setTag(watcher);
        }

        helper.addOnClickListener(R.id.text_submit);
    }
}
